package starter.kit.rx.app.feature.book;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import nucleus.factory.RequiresPresenter;
import starter.kit.feature.NetworkContract;
import starter.kit.feature.rx.RxStarterActivity;
import starter.kit.retrofit.ErrorResponse;
import starter.kit.retrofit.RetrofitException;
import starter.kit.rx.app.R;
import starter.kit.rx.app.model.bean.book.Books;
import starter.kit.rx.app.util.DisplayImgUtis;
import starter.kit.rx.app.views.html5.HTML5WebViewActivity;
import work.wanghao.simplehud.SimpleHUD;

/**
 * ============================================================
 * 版权： xx 版权所有（c）2016
 * <p>
 * 作者：Loofer
 * 版本：1.0
 * 创建日期 ：2016/10/26 19:10.
 * 描述：
 * <p>
 * 注:如果您修改了本类请填写以下内容作为记录，如非本人操作劳烦通知，谢谢！！！
 * Modified Date Modify Content:
 * <p>
 * ==========================================================
 */
@RequiresPresenter(BookDetailPresenter.class)
public class BookDetailActivity extends RxStarterActivity<BookDetailPresenter> implements NetworkContract.View {

    @BindView(R.id.iv_music)
    ImageView mIvMusic;
    @BindView(R.id.tv_book_name)
    TextView mTvBookName;
    @BindView(R.id.tv_book_grade)
    TextView mTvBookGrade;
    @BindView(R.id.tv_book_art)
    TextView mTvBookArt;
    @BindView(R.id.tv_book_publishtime)
    TextView mTvBookPublishtime;
    @BindView(R.id.tv_book_publish_address)
    TextView mTvBookPublishAddress;
    @BindView(R.id.tv_book_grade_num)
    TextView mTvBookGradeNum;
    @BindView(R.id.tv_description)
    TextView mTvDescription;
    @BindView(R.id.tv_author_description)
    TextView mTvAuthorDescription;
    @BindView(R.id.tv_chapters)
    TextView mTvChapters;
    private String mId;//
    private Books mBooks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        if (intent != null) {
            mId = intent.getStringExtra("id");
        }
        getPresenter().requestItem(mId);
    }

    private void initView() {
        if(mBooks.getImages()!=null) {
            DisplayImgUtis.getInstance().display(this, mBooks.getImages().getLarge(), mIvMusic);
        }
        if(!TextUtils.isEmpty(mBooks.getTitle())){
            mTvBookName.setText(mBooks.getTitle());
            setTitle(mBooks.getTitle());
        }
        if(mBooks.getAuthor()!=null&& mBooks.getAuthor().size()>0){
            mTvBookArt.setText(mBooks.getAuthor().get(0));
        }
        if(!TextUtils.isEmpty(mBooks.getPublisher())){
            mTvBookPublishAddress.setText(mBooks.getPublisher());
        }
        if(!TextUtils.isEmpty(mBooks.getPubdate())){
            mTvBookPublishtime.setText("出版时间"+mBooks.getPubdate());
        }
        if(!TextUtils.isEmpty(mBooks.getPublisher())){
            mTvBookPublishAddress.setText(mBooks.getPublisher());
        }

        if(!TextUtils.isEmpty(mBooks.getSummary())){
            mTvDescription.setText(mBooks.getSummary());
        }
        if(!TextUtils.isEmpty(mBooks.getAuthor_intro())){
            mTvAuthorDescription.setText(mBooks.getAuthor_intro());
        }
        if(!TextUtils.isEmpty(mBooks.getCatalog())){
            mTvChapters.setText(mBooks.getCatalog());
        }
        if(!TextUtils.isEmpty(mBooks.getRating().getAverage())){
            mTvBookGrade.setText(mBooks.getRating().getAverage()+"分");
        }
        if(!TextUtils.isEmpty(""+mBooks.getRating().getNumRaters())){
            mTvBookGradeNum.setText(mBooks.getRating().getNumRaters()+"人评");
        }
    }

    @OnClick({R.id.tv_want_read, R.id.tv_more_info, R.id.rl_author})
    public void onClick(View view) {
        Intent  intent;
        switch (view.getId()) {
            case R.id.tv_want_read:
            case R.id.tv_more_info:
                intent=new Intent(this, HTML5WebViewActivity.class);
                intent.putExtra(HTML5WebViewActivity.EXTRA_URL,mBooks.getAlt());
                startActivity(intent);
                break;
            case R.id.rl_author:
                intent=new Intent(this, HTML5WebViewActivity.class);
                intent.putExtra(HTML5WebViewActivity.EXTRA_URL,mBooks.getAlt());
                startActivity(intent);
                break;
        }
    }


    @Override
    public void onSuccess(Object item) {
        mBooks = (Books) item;
        initView();
    }

    @Override
    public void onError(RetrofitException exception) {
        try {
            ErrorResponse errorResponse = exception.getErrorBodyAs(ErrorResponse.class);
            SimpleHUD.showErrorMessage(this, errorResponse.getMessage());
        } catch (IOException e) {
            SimpleHUD.showErrorMessage(this, exception.getMessage());
        }
    }


}
